import { updateUserUsingPost } from '@/services/backend/userController';
import { ProColumns, ProTable } from '@ant-design/pro-components';
import '@umijs/max';
import { message, Modal } from 'antd';
import React from 'react';
import {updateAnnouncementUsingPost} from "@/services/backend/announcementController";

interface Props {
  oldData?: API.Announcement;
  modalVisible: boolean;
  columns: ProColumns<API.Announcement>[];
  onSubmit: () => void;
  onCancel: () => void;
}

/**
 * @en-US Update node
 * @zh-CN 更新节点
 *
 * @param fields
 */
const handleUpdate = async (fields: API.AnnouncementUpdateRequest) => {
  const hide = message.loading('正在更新');
  try {
    await updateAnnouncementUsingPost({
      ...fields,
    });
    hide();
    message.success('更新成功');
    return true;
  } catch (error: any) {
    hide();
    message.error(`更新失败${error.message}`);
    return false;
  }
};

const UpdateModal: React.FC<Props> = (props) => {
  const { modalVisible, columns, onSubmit, onCancel, oldData } = props;

  return (
    <Modal title={'更新'} open={modalVisible} destroyOnClose footer={null} onCancel={onCancel}>
      <ProTable
        type={'form'}
        columns={columns}
        form={{ initialValues: oldData }}
        onSubmit={async (value) => {
          const success = await handleUpdate({
            ...value,
            id: oldData?.id,
          });
          if (success) {
            onSubmit?.();
          }
        }}
      />
    </Modal>
  );
};
export default UpdateModal;
